package com.gouaixin.bee.beeframework2.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.ab.util.AbImageUtil;
import com.ab.util.AbToastUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.gouaixin.bee.beeframework2.MyAPI;
import com.gouaixin.bee.beeframework2.R;
import com.gouaixin.bee.beeframework2.bean.FENSI;
import com.gouaixin.bee.beeframework2.tool.PreferencesUtils;
import com.gouaixin.bee.beeframework2.tool.mLog;
import com.gouaixin.bee.beeframework2.bean.BaseModel;
import com.lidroid.xutils.http.RequestParams;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.ImageLoadingListener;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

public class guanzhuwoAdapter2 extends BaseAdapter {


    Context context;
    //	ArrayList<HashMap<String, Object>> al;
    LayoutInflater mInflater;
    JSONArray jsonArray;
    JSONObject jsonOb;

    ArrayList<FENSI>  data;

    boolean isZhaohu[] = new boolean[100];//是否已经打过招呼
    ImageLoader imageLoader;

    public guanzhuwoAdapter2(Context context) {
        this.context = context;
//		this.al = al;
        mInflater = LayoutInflater.from(context);
        this.imageLoader=ImageLoader.getInstance();
        setJson(new JSONArray());
    }

    public void setJson(JSONArray jsonArray) {
        this.jsonArray = jsonArray;
        this.data = new Gson().fromJson(jsonArray.toString(), new TypeToken<ArrayList<FENSI>>() {}.getType());
    }


    public guanzhuwoAdapter2(Context context, ArrayList<HashMap<String, Object>> al) {
        this.context = context;
//		this.al = al;
        mInflater = LayoutInflater.from(context);

    }

    public int getCount() {
        return jsonArray.length();
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return position;
    }

    ViewHolder viewHolder;


    public View getView(final int position, View convertView, ViewGroup parent) {


//        if (convertView == null) {

        convertView = mInflater.inflate(R.layout.item_guanzhu, null);
        viewHolder = new ViewHolder();
        viewHolder.icon = (ImageView) convertView.findViewById(R.id.icon);
        viewHolder.name = (TextView) convertView.findViewById(R.id.name);
        viewHolder.age = (TextView) convertView.findViewById(R.id.age);
        viewHolder.photonumber = (TextView) convertView.findViewById(R.id.xiangce);
        viewHolder.guanzhu = (TextView) convertView.findViewById(R.id.isGuanzhu);

//        convertView.setTag(viewHolder);

//        } else {
//
//            viewHolder = (ViewHolder) convertView.getTag();
//        }


        mLog.e("关注我的解析：" + data.size());


        final FENSI bean=data.get(position);


        imageLoader.loadImage(bean.getIcon(),new ImageLoadingListener() {
            @Override
            public void onLoadingStarted(String s, View view) {

            }

            @Override
            public void onLoadingFailed(String s, View view, FailReason failReason) {

            }

            @Override
            public void onLoadingComplete(String s, View view, Bitmap bitmap) {
//                imageLoader.displayImage(com.ytr.mymodule.app.bean.getIcon(),icon);
                viewHolder.icon.setBackgroundDrawable(AbImageUtil.bitmapToDrawable(bitmap));
            }

            @Override
            public void onLoadingCancelled(String s, View view) {

            }
        });



        viewHolder.name.setText(bean.getName());
        viewHolder.age.setText(bean.getAge());
        viewHolder.photonumber.setText(bean.getPhoto().size()+"照片");

        final String cid = PreferencesUtils.getString(context, "id", "");
        viewHolder.guanzhu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BaseModel.setCallBackLister(new BaseModel.CallBackLister() {
                    @Override
                    public String onCallBack(String url, String data) {
                        AbToastUtil.showToast(context, "添加关注成功");
                        viewHolder.guanzhu.setText("已添加关注");
                        PreferencesUtils.putBoolean(context,bean.getId()+"isGuanzhu",true);
                        notifyDataSetChanged();
                        return url;
                    }
                });
                RequestParams params=new RequestParams();
                params.addBodyParameter("id",bean.getId());
                params.addBodyParameter("cid", cid);
                BaseModel.post(MyAPI.addguanzhu,params);
            }
        });


        final boolean isGuanzhu=PreferencesUtils.getBoolean(context,bean.getId()+"isGuanzhu",false);

        if(isGuanzhu==false){
            viewHolder.guanzhu.setText("关注TA");

        }else {
            viewHolder.guanzhu.setText("已添加关注");
        }



//        try {
//            if (jsonArray.length() > 0) {
//
//
//                jsonOb = (JSONObject) jsonArray.opt(position);
//                if (jsonOb != null) {
//                    String icon = jsonOb.getString("icon");
//                    if(!icon.equals("")){
//                        ImageLoader.getInstance().displayImage(icon, viewHolder.icon);
//                    }else {
//                        viewHolder.icon.setBackgroundResource(R.drawable.ic_other_avatar_status02);
//                    }
//                    viewHolder.name.setText(jsonOb.getString("name"));
//                    viewHolder.age.setText(jsonOb.getString("age"));
//                    }
//                }
//            }
//        } catch (JSONException e) {
//            mLog.e("json解析异常"+e.getMessage());
//            e.printStackTrace();
//        }

        return convertView;
    }


    /**
     *
     */
    class ViewHolder {
        ImageView icon;
        TextView name;
        TextView age;
        TextView photonumber;
        TextView guanzhu;

    }


}
